package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 会员SERVICE实现类
 */
/*任务8 手机验证码登录 肖丁豪*/
@Service
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    /**
     * 任务10-手机端提交预约-添加会员
     * @param member
     */
    @Override
    public Member createOrUpdate(Member member) {
        log.info("正在添加会员：{}", member);
        Member pojo = memberDao.selectByPhoneNumber(member.getPhoneNumber());
        if (pojo != null) {
            //会员已存在 更新
            member.setId(pojo.getId());
            memberDao.updateByIdSelective(member);
        } else {
            //不存在 插入
            member.setRegTime(new Date());
            memberDao.insert(member);
        }
        //返回会员数据
        return memberDao.selectById(member.getId());
    }


    /**
     * 任务13- 统计分析-会员数量折线图
     * @param months
     * @return
     */
    @Override
    public List<Integer> findCountMember(List<String> months) {
        //返回值是List<Integer>,定义一个集合
        List<Integer> memberCount = new ArrayList<>();
        //遍历月份
        for (String month : months) {
            //不能直接查询出当月份的数量只能查询最后月份之前的所有数据
            String endDate = month+".31";
            Long count = memberDao.selectEndCountMember(endDate);
            //集合添加
            memberCount.add(count.intValue());
        }
        return memberCount;
    }
}
